package Datastructures.SparseArray;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class SparseArray {
    public static void main(String[] args)  {
        //先创建一个原始二维数组
        int chessArr1[][]=new int[11][11];
        //0表示没有棋子 1为黑、2为白；
        chessArr1[1][2]=1;
        chessArr1[2][3]=2;
        System.out.println("------------------");
        for(int i=0;i<11;i++){
            for(int j=0;j<11;j++){
                System.out.printf("%d\t",chessArr1[i][j]);
            }
            System.out.println();
        }
        //将二维数组转换稀疏矩阵
        //1、先遍历二维数组得到非0数据个数
        int sum=0;
        for(int i=0;i<11;i++){
            for(int j=0;j<11;j++) {
                if(chessArr1[i][j]!=0){
                        sum++;
                        }
                    }
            }
        //2、创建对应稀疏数组
        int spareArr[][]=new int[sum+1][3];
        spareArr[0][0]=11;
        spareArr[0][1]=11;
        spareArr[0][2]=sum;
        //遍历二维数组将非0值存入稀疏数组中
        int count=0;//利用一个计数器来完成对稀疏数组行元素的动态变化
        for(int i=0;i<11;i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    spareArr[count][0] = i;
                    spareArr[count][1] = j;
                    spareArr[count][2] = chessArr1[i][j];
                }
            }
        }
            //输出稀疏数组
            System.out.println();
            System.out.println("=========");
            for(int i=0;i<spareArr.length;i++){
                System.out.printf("%d\t%d\t%d\t\n",spareArr[i][0],spareArr[i][1],spareArr[i][2]);
            }
            System.out.println();
     //稀疏数组转换为原始数组
        //1、先读取稀疏数组的第一行，构建出初始原始数组，其中全为零
        int chessArr2[][]=new int[spareArr[0][0]][spareArr[0][1]];
        //2、读取稀疏数组的后几行数据，构建原始二维数组中相应的值
        for(int i=1;i<spareArr.length;i++){
            chessArr2[spareArr[i][0]][spareArr[i][1]]=spareArr[i][2];
        }
            //输出
        System.out.println();
        System.out.println("=========");

        for(int i=0;i<11;i++) {
            for (int j = 0; j < 11; j++) {
                System.out.printf("%d\t",chessArr2[i][j]);
                }
            System.out.println();
        }
        //四、将稀疏数组保存到磁盘上，如map.data
        try {
            System.out.println("将稀疏数组保存到磁盘并命名为map.wyk");
            File f = new File("E:\\out\\map.wyk");
            FileOutputStream fos = new FileOutputStream(f);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
            System.out.println("写入中----------");
            for(int i = 0; i < spareArr.length; i++) {
                osw.write(spareArr[i][0] + "," + spareArr[i][1] + "," + spareArr[i][2] + ",");
            }
            osw.close();//关闭输出流
            fos.close();//关闭输出流
            System.out.println("写入磁盘成功");
            //读取磁盘中的map.data文件
            System.out.println("读取中----------");
            FileInputStream fis = new FileInputStream(f);
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            StringBuffer sb = new StringBuffer();
            while(isr.ready()) {
                sb.append((char)isr.read());
                System.out.println(sb);
            }
            isr.close();//关闭输入流
            fis.close();//关闭输入流
            System.out.println("读取成功");
            String ss = sb.toString();
            String[] sb1 = sb.toString().split(",");
            System.out.printf("从磁盘读取的字符串为：\n%s\n", ss);//格式化输出
            //恢复稀疏数组
            int sum1 = 0;
            int[][] sparseArr1 = new int[sb1.length/3][3];
            sparseArr1[0][0] = Integer.parseInt(sb1[0]);
            sparseArr1[0][1] = Integer.parseInt(sb1[1]);
            sparseArr1[0][2] = Integer.parseInt(sb1[2]);
            for(int i = 3; i < sb1.length; i += 3) {
                sum1++;
                sparseArr1[sum1][0] = Integer.parseInt(sb1[i]);
                sparseArr1[sum1][1] = Integer.parseInt(sb1[i+1]);
                sparseArr1[sum1][2] = Integer.parseInt(sb1[i+2]);
            }
            System.out.println("还原后的稀疏数组为：");
            for(int i = 0; i < sparseArr1.length; i++) {
                System.out.printf("%d\t%d\t%d\n", sparseArr1[i][0], sparseArr1[i][1], sparseArr1[i][2]);
            }
            //恢复原始二维数组
            int[][] chessArr3 = new int[sparseArr1[0][0]][sparseArr1[0][1]];
            for(int i = 1; i < sparseArr1.length; i++) {
                chessArr3[sparseArr1[i][0]][sparseArr1[i][1]] = sparseArr1[i][2];
            }
            System.out.println("还原后的二维数组为:");
            for(int[] a : chessArr3) {
                for(int b : a) {
                    System.out.printf("%d\t", b);
                }
                System.out.println();
            }
        }catch(IOException e) {
            e.printStackTrace();
        }

        }
    }
